<template>
    <mi-form-page @submit="submit"
                  title="店长信息编辑"
                  v-model:show="edit.show"
                  :loading="edit.loading"
    >
        <el-form :model="edit.info"
                 ref="formRef"
                 :rules="rules">

            <el-form-item prop="user_id" label="所属会员">
                <user-select v-model="edit.info.user_id" />
            </el-form-item>

            <el-form-item prop="image" label="店铺图片">
                <mi-up-image v-model="edit.info.image" />
            </el-form-item>

            <el-form-item prop="title" label="店铺名称">
                <mi-input v-model="edit.info.title" />
            </el-form-item>

            <el-form-item prop="realname" label="店长姓名">
                <mi-input v-model="edit.info.realname" />
            </el-form-item>

            <el-form-item prop="mobile" label="手机号">
                <mi-input v-model="edit.info.mobile" />
            </el-form-item>

            <el-form-item prop="status" label="状态">
                <mi-switch v-model="edit.info.status" />
            </el-form-item>

            <el-form-item prop="parent_id" label="上级店长">
                <store-manager-select v-model="edit.info.parent_id" />
            </el-form-item>

        </el-form>
    </mi-form-page>
</template>

<script setup lang="ts">
import {reactive, watch, ref, onBeforeMount, inject} from "vue";
import {makeModelAuth} from "@/utils/auth";
import List from "@/model/list";
import Edit from "@/model/edit";
import {FormInstance} from "element-plus";
import {simpleGet, simplePost} from "@/utils/request";
import {useAdminStore} from "@/store/admin";
import UserSelect from "@/views/site/components/UserSelect.vue";
import StoreManagerSelect from "@/views/store/components/StoreManagerSelect.vue";

const formRef = ref<FormInstance>()
const auth = makeModelAuth("store.storeManager")
const rules = {
    user_id:[{required:true, message:'必填'}],
    title:[{required:true, message:'必填'}],
    image:[{required:true, message:'必填'}],
    mobile:[{required:true, message:'必填'}],
    realname:[{required:true, message:'必填'}],
}
const adminStore = useAdminStore()
const list = inject<List>("list")
const edit = inject<Edit>("edit", new Edit({}))

const submit = () => {
    let url = '/' + (edit.info.id ? auth.edit : auth.add)
    edit.submit({
        formRef: formRef.value,
        url: url,
        list: list
    })
}

watch(() => edit.show, (nv, ov) =>{
    if (nv){
        edit.init(async () => {
            const id = edit.info.id
            if (id){
                // 请求表单数据
                let res = await simpleGet('/' + auth.edit, {id})
                edit.info = res.info
            }else{
                edit.info = {
                    status:1,
                    site_id: adminStore.site.id
                }
            }
        })
    }
})

</script>

<style scoped>

</style>
